package com.tkjt.pms.common.utils;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class DoubleUtil {

    private static final int DEF_DIV_SCALE = 8;

    /**
     * * 两个Double数相加 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double add(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.add(b2).doubleValue();
    }
    public static Double add(Double v1, Double v2, Double v3) {
        return add(add(v1,v2),v3);
    }

    /**
     * * 两个Double数相减 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double sub(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.subtract(b2).doubleValue();
    }

    /**
     * * 两个Double数相乘 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double mul(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.multiply(b2).doubleValue();
    }

    /**
     * * 两个Double数相除 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     * 默认保留两位小数
     */
    public static Double div(Double v1, Double v2) {
        if (Double.isNaN(v2) || Double.isInfinite(v2)|| Double.isInfinite(v1)) {
            return 0.00;
        }
        if (v2==0) {
            return 0.00;
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * * 两个Double数相除，并保留scale位小数 *
     *
     * @param v1 *
     * @param v2 *
     * @param scale *
     * @return Double
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (Double.isNaN(v2) || Double.isInfinite(v2)|| Double.isInfinite(v1)) {
            return 0.00;
        }
        if (v2==0) {
            return 0.00;
        }
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static Double getDouble(Double v1, int scale) {
        return  new BigDecimal(v1).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 保留 scale 位小数
     * 含百分号的保留百分号里面 scale 位小数
     */
    public static String getDouble(Object o1, int scale) {
        if(o1 == null){return null;}
        try {
            String str = o1.toString();
            if(str.contains("%")){
                return new BigDecimal(str.substring(0,str.length()-2)).setScale(scale, BigDecimal.ROUND_HALF_UP).toString()+"%";
            }else{
                return  new BigDecimal(str).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
            }
        }catch (Exception e){
            return (String)o1;
        }
    }
    /**
     * 所有数据保留两位小数
     * 含百分比的转为小数 保留4位
     * 适用于 绩效目标值 页面展示数据
     */
    public static String getDouble(Object o1) {
        if(o1 == null){return null;}
        try {
            String str = o1.toString();
            if(str.contains("%")){
                return new BigDecimal(str.substring(0,str.length()-2)).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
            }else{
                return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            }
        }catch (Exception e){
            return (String)o1;

        }
    }

    public static Double getDouble(Double v1) {
        return  new BigDecimal(v1).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 初始化公式入参数据
     * @param v1
     * @return
     */
    public static Double toD(Double v1) {
        if (v1 == null){
            return 0.0;
        }
        return  v1;
    }

    /**
     * 获取两数相处并转化为百分比
     * @return
     */
    public static String divPercent(Double v1, Double v2){
        Double rate=div(v1,v2);
        NumberFormat num = NumberFormat.getPercentInstance();
//        num.setMaximumIntegerDigits(IntegerDigits);//小数点前保留几位
//        num.setMinimumFractionDigits(FractionDigits);// 小数点后保留几位
        return  num.format(rate);
    }
    public static String divPercent(int v1, int v2){
        Double rate=div((double) v1, (double) v2);
        NumberFormat num = NumberFormat.getPercentInstance();
        //num.setMaximumIntegerDigits(IntegerDigits);//小数点前保留几位
        num.setMinimumFractionDigits(2);// 小数点后保留几位
        return  num.format(rate);
    }

    public static String formateForDouble(String data) {
        if(data == null)return "0.0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
    public static String formateForDouble(Double data) {
        if(data == null)return "0.0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }
    public static String formateForDoubleScale1(String data) {
        if(data == null)return "0.0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
    }
    public static String formateForDoubleScale0(Double data) {
        if(data == null)return "0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(0, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String formateForDoubleScale1(Double data) {
        if(data == null)return "0";
        BigDecimal bg = new BigDecimal(data);
        return bg.setScale(1, BigDecimal.ROUND_HALF_UP).toString();
    }
}
